package com.sc.sysconfig.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.sysconfig.dao.SysUserMapper;
import com.sc.sysconfig.dto.UserLoginDTO;
import com.sc.sysconfig.dto.UserQueryDTO;
import com.sc.sysconfig.entity.SysUser;
import com.sc.sysconfig.service.SysUserService;
import com.sc.sysconfig.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 系统用户服务impl
 *
 * @author 徐赫阳
 * @date 2021/09/02
 */
@Service
@ComponentScan
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    /**
     * 系统用户映射器
     */
    @Autowired
    SysUserMapper sysUserMapper;

    /**
     * 登录
     *
     * @param user 用户
     * @return {@link SysUser}
     */
    @Override
    public SysUser login(UserLoginDTO user){
        return this.sysUserMapper.login(user);
    }

    /**
     * 获取用户列表
     *
     * @param queryDTO 查询dto
     * @return {@link PageVO}<{@link SysUser}>
     */
    @Override
    public PageVO<SysUser> getUserList(UserQueryDTO queryDTO){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysUser::getIsDel,0)
                .like(queryDTO.getName() != null,SysUser::getName,queryDTO.getName())
                .eq(queryDTO.getStatus()!= null,SysUser::getStatus,queryDTO.getStatus())
                .ge(queryDTO.getBirthday() != null,SysUser::getBirthday,queryDTO.getBirthday())
                .orderByAsc(SysUser::getId);
        Integer count = this.sysUserMapper.selectList(queryWrapper).size();
        List<SysUser> list = this.sysUserMapper.selectList(queryWrapper.lambda().last("LIMIT "+ (queryDTO.getPage() - 1) * queryDTO.getPageSize() + ","+ queryDTO.getPageSize()));
        return PageVO.setPage(queryDTO.getPage(),queryDTO.getPageSize(),list,count);
    }


}
